import { defineStore } from 'pinia'
import { ref } from 'vue'
import type { LoginDetail, LoginResult } from '@/types/member'

export const useUserStore = defineStore(
  'user',
  () => {
    // 用户信息
    const profile = ref<LoginResult | LoginDetail>()

    // 存储用户的登录数据
    const saveProfile = (data: LoginResult | LoginDetail) => {
      profile.value = data
    }
    // 退出登录清空信息
    const clear = () => {
      profile.value = undefined
    }
    return {
      profile,
      saveProfile,
      clear
    }
  },
  {
    persist: {
      storage: {
        setItem(key, value) {
          uni.setStorageSync(key, value)
        },
        getItem(key) {
          return uni.getStorageSync(key)
        }
      }
    }
  }
)
